import {
  searchMember,
  exchangeCard
} from '../../apis/group'
const app = getApp();

Page({
  data: {
    memberList: [],
    pageNum: 1,
    pageSize: 10,
    isHaveMore: true,
    isRequest: false,
    searchList: [],
    isSearch: false,
    inputValue: ''
  },

  onLoad(options) {
    this.setData({
      ...options
    });
  },

  onReachBottom() {
    if (this.data.isHaveMore) {
      this.setData({
        pageNum: this.data.pageNum + 1
      });
      this.searchApi(this.data.pageNum);
    };
  },

  getGroupMemberList(id, pageNum, pageSize, type) {
    getGroupMemberList(id, pageNum, pageSize).then(res => {
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('https://').test(v.avatar) && !new RegExp('http://').test(v.avatar)) v.avatar = app.globalData.url + v.avatar;
          if (v.imageList) v.imageList = v.imageList.map(val => {
            if (!new RegExp('https://').test(val) && !new RegExp('http://').test(val)) val = app.globalData.url + val;
            return val
          });
        });
        if (type === 'refresh') {
          this.setData({
            memberList: rows
          });
        } else {
          rows.forEach(v => {
            this.data.memberList.push(v);
          });
          this.setData({
            memberList: this.data.memberList
          });
        };
        if (this.data.memberList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },

  selectCard(e) {
    let {
      id,
      index
    } = e.currentTarget.dataset;
    wx.showModal({
      content: '是否交换名片',
      title: '交换名片',
      success: res => {
        if (res.confirm) {
          exchangeCard(this.data.groupId, id).then(res => {
            if (res.data.code === 200) {
              wx.showToast({
                title: '已发送',
                icon: 'success',
                duration: 1500,
                mask: true
              });
              this.data.memberList[index].agreeStatus = 2;
              this.setData({
                memberList: this.data.memberList
              });
            };
          });
        };
      }
    });
  },

  previewCard(e) {
    let {
      id
    } = e.currentTarget.dataset;
    wx.navigateTo({
      url: `/pages/cardPreview/index?from=group&cardId=${id}&groupId=${this.data.groupId}`
    });
  },

  // 输入框
  handleInput(e) {
    let {
      value
    } = e.detail;
    this.setData({
      inputValue: value
    });
  },
  searchApi(pageNum, type) {
    if (this.data.isRequest) return
    this.setData({
      isRequest: true
    });
    searchMember(this.data.groupId, pageNum, 10, this.data.inputValue).then(res => {
      this.setData({
        isRequest: false
      });
      if (res.data.code === 200) {
        let {
          rows,
          total
        } = res.data;
        rows.forEach(v => {
          if (!new RegExp('^https://').test(v.avatar) && !new RegExp('^http://').test(v.avatar)) app.globalData.url + v.avatar;
        });
        if (type === 'refresh') {
          this.setData({
            searchList: rows
          });
        } else {
          rows.forEach(v => {
            this.data.searchList.push(v);
          });
          this.setData({
            searchList: this.data.searchList
          });
        };

        if (this.data.searchList.length === total) {
          this.setData({
            isHaveMore: false
          });
        };
      };
    });
  },
  // 搜索
  confirmSearch() {
    if (this.data.inputValue === '') {
      wx.showToast({
        title: '请输入名称',
        icon: 'none'
      });
      return
    };
    this.setData({
      pageNum: 1,
      isHaveMore: true
    });

    this.searchApi(1, 'refresh');
    // let arr = [];
    // this.data.memberList.forEach(v => {
    //   if (new RegExp(`${this.data.inputValue}`).test(v.userName) && !v.owner) {
    //     arr.push(v);
    //   };
    // });
    // this.setData({
    //   isSearch: true,
    //   searchList: arr
    // });
  },
  // 取消搜索
  cancelSearch() {
    this.setData({
      inputValue: '',
      searchList: [],
      isSearch: false
    });
  },

  bindKeyInput(e) {
    this.setData({
      inputValue: e.detail.value
    });
    if (this.data.inputValue != '') {
      this.checkColor();
    };
  },

  checkColor() {
    console.log("-")
    let that = this;
    let value = that.data.inputValue;
    // 将标题已关键字拆开成数组
    for (let i = 0; i < that.data.lists.length; i++) {
      let title = 'lists[' + i + '].title';
      let returnData = that.hilight_word(value, that.data.lists[i].title);
      if (that.data.lists[i].tag) {
        for (let k = 0; k < that.data.lists[i].tag.length; k++) {
          let tag = 'lists[' + i + '].tag[' + k + ']';
          let returntag = that.hilight_word(value, that.data.lists[i].tag[k]);
          that.setData({
            [tag]: returntag
          });
        };
      };
      that.setData({
        [title]: returnData
      });
    };
  },
  // 根据搜索字分割字符
  hilight_word(key, word) {
    let idx = word.indexOf(key),
      t = [];
    if (idx > -1) {
      if (idx == 0) {
        t = this.hilight_word(key, word.substr(key.length));
        t.unshift({
          key: true,
          str: key
        });
        return t;
      }

      if (idx > 0) {
        t = this.hilight_word(key, word.substr(idx));
        t.unshift({
          key: false,
          str: word.substring(0, idx)
        });
        return t;
      }
    }
    return [{
      key: false,
      str: word
    }];
  }
})